﻿using SharPy.Interface;
using System;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.UI.Xaml.Controls;

namespace SharPy.Parser.clCommands
{
    class HideImage : clCommand
    {
        public static bool RegExpCheck(string s)
        {
            if (Regex.IsMatch(s, "^hide(.*)with(.*)"))
            {
                return true;
            }
            else if (Regex.IsMatch(s, "^hide(.*)"))
            {
                return true;
            }
            else
                return false;
        }

        public static clCommand Parse(string s)
        {
            if (Regex.IsMatch(s, "^hide(.*)with(.*)"))
            {
                MatchCollection m = Regex.Matches(s, @"hide(.*)with(.*)");
                return (new clCommand(clCommand.Name.HideImage
                                        , m[0].Groups[1].Value.Trim()
                                        , m[0].Groups[2].Value.Trim()
                                      ));
            }
            else if (Regex.IsMatch(s, "^hide(.*)"))
            {
                MatchCollection m = Regex.Matches(s, @"hide(.*)");
                return (new clCommand(clCommand.Name.HideImage
                                        , m[0].Groups[1].Value.Trim()
                                        , null
                        ));
            }
            else throw new ArgumentException();
        }

        public static async Task<int> CommandHandle(clCommand objCom, GamePage page)
        {
            Grid gridMain = (Grid)page.FindName("gridMain");

            Image a = (Image)page.FindName(objCom.sParameter[0]);
            if (a == null)
            {
                foreach (Windows.UI.Xaml.UIElement i in gridMain.Children)
                {
                    if (i is Image)
                    {
                        var im = i as Image;
                        if (im.Name.Contains(objCom.sParameter[0]))
                            a = i as Image;
                    }
                }
            }
            if (objCom.sParameter[1] != null)
            {
                switch (objCom.sParameter[1])
                {
                    case "fade":
                        await clAnimations.FadeOut(page, a, 0.2);
                        break;
                    case "dissolve":
                        await clAnimations.FadeOut(page, a, 0.2);
                        break;
                    default:
                        break;
                }
            }
            else
                gridMain.Children.Remove(a);
            return 0;
        }
    }
}
